--
-- Author: ping.wang
-- Date: 2019-05-21 19:05:13
--
-- DailyRoutineRender
--

local ClassRef = app.mvc.createListViewCell(...)
local BgColorTable = {
    cc.c3b(255,221,103),
    cc.c3b(255,168,0),
    cc.c3b(248,160,138),
    cc.c3b(144,230,255),
    cc.c3b(1,255,167),
}
local TextColorTable = {
    cc.c3b(255,225,132),
    cc.c3b(255,168,0),
}
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DailyRoutineRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._ui.Panel:setAnchorPoint(cc.p(0,0))
    self._ui.Panel:setPosition(cc.p(0,0))
    self._ui.mask:setVisible(false)
    self._ui.tips:setVisible(false)

    self._ui.Panel:addClickEventListener(function()
        --准备跳转到相应的功能界面 但是锁住的 未开放的不能点
        if not self._data.islock then
            local p = string.split(GD:getConvertResource(self._data.conf.resourceID), "|")
            display.resourceGotoFeature({feature=p[1], param={p[3]}})
        end
    end)

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

--{ID = 1,IntPar = 1,Max = 2,State = false,conf = conf}
function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data.conf
        self._ui.tlt:setString(L(data.name))
        self._ui.Image:loadTexture(string.format("art/timeline/%s.png",data.icon))
        self._ui.bg:setColor(BgColorTable[checknumber(data.color)])
        self._ui.tlt:setTextColor(TextColorTable[checknumber(data.nameColor)])

        local needReOrderNodes = {}
        needReOrderNodes[1] = self._ui["Panel_star/tlt"]

        for i = 1, 5 do
            local isVisible = checknumber(data.star) >= i
            local starNode = self._ui["Panel_star/s"..i]
            starNode:setVisible(isVisible)
            if isVisible then
                table.insert(needReOrderNodes,starNode)
            end
        end

        self:setNodesMiddle(self._ui["Panel_star"],0,needReOrderNodes)
        if self._data.islock then
            self._ui.Panel_suo:setVisible(true)
            self._ui.Panel_suo:findChild("tlt_suo"):setString(string.format(L("dailyroutine7"),data.openLevel))
            self._ui.mask:setVisible(true)
        else
            self._ui.Panel_suo:setVisible(false)
            self._ui.mask:setVisible(false)
        end

        if self._data.State then
            self._ui.complete:setVisible(true)
        else
            self._ui.complete:setVisible(false)
        end

        self._ui.num:setVisible(false)

        local type = tonumber(data.type)

        if type == 16 then
            self._ui.tips:setVisible(true)
            self._ui.tips:setString(L("dailyroutine6"))
        else
            self._ui.tips:setVisible(false)
        end

        if type == 6 then
            --冠军赛 1是已报名 0是未报名
            if self._data.IntPar then
                self._ui.num:setVisible(true)
                if self._data.IntPar == 1 then
                    self._ui.num:setString(L("weekroutinename_12"))
                else
                    self._ui.num:setString(L("weekroutinename_13"))
                end
            end
        elseif type == 16 or type == 17 or type == 18 or type == 24 or type == 26 then
            if self._data.Max and self._data.Max > 0 then
                self._ui.num:setVisible(true)
                self._ui.num:setString(string.format("%s:%d/%d",L("dailyroutine13"),self._data.IntPar,self._data.Max))
            end
        elseif type == 8 then
            --决斗赛排名奖励

            if self._data.IntPar then
                self._ui.num:setVisible(true)
                local rankLower = tonumber(GD:getSystemPar("duelGameRankingLower"))
                if self._data.IntPar <= 0 or self._data.IntPar > rankLower then
                    self._ui.num:setString(L("tips_3000406"))
                else
                    self._ui.num:setString(string.format(L("dailyroutine10"),self._data.IntPar))
                end
            end
        elseif type == 9 then
            --挑战赛
            if self._data.IntPar then
                self._ui.num:setVisible(true)

                if self._data.IntPar <= 0 then
                    self._ui.num:setString(L("tips_3000406"))
                else
                    self._ui.num:setString(string.format(L("dailyroutine10"),self._data.IntPar))
                end
            end

        elseif type == 10 then
            --杀怪经验
            if self._data.IntPar then
                self._ui.num:setVisible(true)
                self._ui.num:setString(string.format(L("dailyroutine5"),self._data.IntPar/100))
            end
        else
            if self._data.Max and self._data.Max > 0 then
                self._ui.num:setVisible(true)
                self._ui.num:setString(string.format("%d/%d",self._data.IntPar,self._data.Max))
            end
        end

    else
        self:setVisible( false )
    end
end


function ClassRef:setNodesMiddle(parentNode, xPadding, nodes)
    if not parentNode then return end
    local count = #nodes
    local totalLength = 0
    local lengths = {}

    for i = 1, count do
        local node = nodes[i]
        node:setAnchorPointX(0.5)
        lengths[i] = node:getContentSize().width * node:getScaleX()
        totalLength = totalLength + lengths[i]
    end

    local size = parentNode:getContentSize().width
    local interval = xPadding or 0 --每两个node之间的间距
    totalLength = totalLength + (count - 1) * interval

    local leftLen = 0
    for i = 1, count do
        local rightLen = totalLength / 2 - leftLen - lengths[i]
        local posX = size / 2 - rightLen - lengths[i] / 2
        nodes[i]:setPositionX(posX)

        leftLen = leftLen + lengths[i] + interval
    end

end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
